package com.xiaochen.sjzremind.controller;

import com.google.common.cache.Cache;
import com.xiaochen.sjzremind.common.Response;
import com.xiaochen.sjzremind.entity.AmmoInfo;
import com.xiaochen.sjzremind.service.IAmmoService;
import com.xiaochen.sjzremind.service.IRedisService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RSet;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author xc
 * @date 2025/10/09/15:42
 * @desc 弹药控制类
 */
@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/ammo")
public class AmmoController implements IAmmoService {
    
    @Resource
    private Cache<String, Integer> ammoInfoCache;
    
    @Resource
    private IRedisService redisService;
    
    @Override
    public Integer getAmmoIdByName(String name) {
        return ammoInfoCache.getIfPresent(name);
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @Override
    public Response<List<AmmoInfo>> getAllAmmoInfo() {
        if (ammoInfoCache.size() == 0) {
            return Response.<List<AmmoInfo>>builder()
                    .code("200")
                    .info("获取失败")
                    .data(null)
                    .build();
        }
        return Response.<List<AmmoInfo>>builder()
                .code("200")
                .info("获取成功")
                .data(ammoInfoCache.asMap()
                        .entrySet()
                        .stream()
                        .map(entry -> AmmoInfo.builder()
                                .id(entry.getValue())
                                .name(entry.getKey())
                                .build()).collect(Collectors.toList()))
                .build();
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @Override
    public Response<Boolean> updateConcernedAmmo(String id, String name) {
        Integer ammoId = getAmmoIdByName(name);
        RSet<String> set = redisService.getSet("user:" + id + ":concernedAmmo");
        boolean contains = set.contains(ammoId);
        if (contains){
            return Response.<Boolean>builder()
                    .code("200")
                    .info("已关注")
                    .data(false)
                    .build();
        }
        redisService.addToSet("user:" + id + ":concernedAmmo", String.valueOf(ammoId));
        return Response.<Boolean>builder()
                .code("200")
                .info("添加成功")
                .data(true)
                .build();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @Override
    public Response<Boolean> deleteConcernedAmmo(String id, String name) {
        Integer ammoId = getAmmoIdByName(name);
        RSet<String> set = redisService.getSet("user:" + id + ":concernedAmmo");
        set.remove(ammoId);
        return Response.<Boolean>builder()
                .code("200")
                .info("删除成功")
                .data(true)
                .build();
    }


}
